<?php
	include_once('../Inc/Global.php');
	session_start();
	
	
function IsLoggedIn()
{
	return isset($_SESSION['UserID']);
}
function GetUserName()
{
	return $_SESSION['UserName'];
}
function GetUserID()
{
	return $_SESSION['UserID'];
}
function GetUserClearance()
{
	return $_SESSION['Security'];
}
function DoLogIn($email, $password)
{
	$sql = "SELECT P.ID, P.First_Name, P.Last_Name, P.Password, P.SecurityLevel " //Space at end is neccesary
		.  "FROM People P Join ConnectionMethods CM ON P.ID=CM.Person_ID "
		.  "WHERE CM.Value ='$email'";
	$conn = getConnection();
	$result = $conn->query($sql);
	$rs = $result->fetch_assoc();
	$conn->close();
	
	if($rs)
	{
		if($rs['Password'] == $password)
		{
			$_SESSION['UserID'] = $rs['ID'];
			$_SESSION['UserName'] = $rs['First_Name'] . " " . $rs['Last_Name'];
			$_SESSION['UserEmail'] = $email;
			$_SESSION['Security'] = $rs['SecurityLevel'];
			return true;
			
		}
		else 
		{
			echo "Incorrect Password.";
		}
	}
	
	return false;  // There were no emails or password was wrong
	
}
function DoLogOut()
{
	session_destroy();
}
function RequireLogIn()
{
	if(!IsLoggedIn()) //If user is not logged in, redirect to login page
	{
		header('location: Login.php');
		die();
	}
}